export const mixin = {
    data() {
        return {
            isHide:true,//是否隐藏主体界面
            disable:true,//是否禁用搜索
            stateShow:false,//是否展示状态组件
            stateDescription:'',//状态组件提示信息
            stateImage:null,//状态组件图片资源
            isloading:true,//是否等待

            keyValue: '',//下拉框选中的值
            searchValue:'',//搜索框的关键词
            queryData:[],//搜索数据集合
            queryFlag:false,//搜索标识

            timer:null,//计时器变量
            list:[],//表格数据
            
            currentpage: 1,//当前页码
            pagesize:20,//一页展示数据总数
            key:0
        }
    },
    watch: {
        keyValue(value){
            // value ? this.disable=false:this.disable=true
            if(value){
                this.disable=false
            }else{
                this.disable=true
                this.searchValue=''
            }
        },
    },
    computed: {
        UserId(){
            return this.$Bmob.User.current().objectId
        },
        RoleId(){
            //返回解密后的角色id
            return this.$Bmob.User.current().role.objectId
        },
        Username(){
            //返回解密后的账号
            return this.$Bmob.User.current().username
        },
        Name(){
            //返回解密后的账号
            return this.$Bmob.User.current().name
        }
    },
    methods: {
        somePermissionList(menuname){
            console.log(this.$store.state.permissionList);
            let permission = this.$store.state.permissionList
            console.log(permission);
            if(typeof permission!=='undefined'&& permission.length!==0){
                console.log('执行这里');
                return permission.some(item => {
                    if(item[menuname] && item[menuname]['permission']['query']) return true
                    return false
                   
                })
            }else{
                console.log(11221);
                this.$http.getRoleById(this,this.RoleId).then(res=>{
                    if(res){
                        let permission1 = this.$store.state.permissionList
                        return permission1.some(item => {
                            if(item[menuname] && item[menuname]['permission']['query']) return true
                            return false
                        })
                    }else{
                        return res
                    }
                })
            }
            
        },
        async checkPermission(menuname){
            return await this.somePermissionList(menuname)?true:false
        },
        //校验身份
        checkUserVerified(){
            return new Promise((resolve,reject)=>{
              this.$http.checkUserData(this,this.UserId).then(data=>{
              if(!data[0].isEnable){
                this.$utils.message.warning(this,'身份验证失败，请重新登陆')
                this.$utils.loginOut(this)
                return true
              }
              this.$http.getRoleById(this,this.RoleId).then(res=>{
                resolve(true)
              }).catch(err=>{
                throw '请重新登录'
              })
            }).catch(error=>{
              console.log(error);
              this.$utils.loginOut(this)
              this.$utils.message.warning(this,'身份验证失败，请重新登陆')
            })
          });
         },
        //按钮点击取消选中状态
        handleClickEvent(e){
            let target = e.target
            if(target.nodeName == 'SPAN'||target.nodeName == 'I'){
                target = e.target.parentNode
            }
            target.blur()
        },
        //重置刷新界面
        resetPagerData(){
            this.stateShow=false
            this.isHide=true
            this.isloading=true
            this.checkUserVerified().then(res=>{
                if(res) {
                    this.checkUserPermission()
                }
            })
        },
        checkUserPermission(){
            this.checkPermission(this.menuname).then(res=>{
                if(res){
                    this.stateShow=false
                    this.isHide=true
                    this.isloading=true
                    //请求数据
                    if(this.$route.query.id){
                        this.getObjectData(this.$route.query.id)
                    }else if(this.$route.query.type){
                        this.getTypeData(this.$route.query.type,this.$route.query.typeid)
                    }else{
                        this.getData(500)
                    }
                }else{
                    this.isloading=false
                    this.isHide=true
                    this.stateShow=true
                    this.stateDescription='暂无权限'
                    this.stateImage=require('@/assets/permission.png')
                }
            });
          
        },
        resetData(){
            // if(this.queryFlag||!this.disable){
                this.queryFlag=false
                // this.moreScreenStatus=false
                this.disable=true
                this.keyValue=''
                this.searchValue=''
                this.queryData.length=0
            //     this.key++
            //     return false
            // }
                this.isHide=true
                this.isloading=true
                this.checkUserVerified().then(res=>{
                    if(res) this.checkUserPermission()
                })
        },
        LoadQueryData(){
            if(this.queryFlag||!this.disable){
                var reg =  new RegExp(this.searchValue,'i');
                this.queryData=this.list.filter(item =>{
                   return reg.test(item[this.keyValue])
                })
            }
            
        },
        resetLoadData(){
            this.isHide=true
            this.isloading=true
            this.checkUserPermission()
            // this.checkUserVerified().then(res=>{
            //     if(res) this.checkUserPermission()
            // })
        },
        handleSizeChange(val) {
          this.pagesize=val
        },
  
        handleCurrentChange(val) {
          this.currentpage=val
        },
        handleSelectionChange(val){
          this.multipleSelection = val;
        },
        searchData(){
            if(this.searchValue){
                this.queryFlag=true
                let keyword = this.keyValue
                this.currentpage=1
                this.pagesize=20
                var reg =  new RegExp(this.searchValue,'i');
                this.queryData=this.list.filter(item =>{
                    return reg.test(item[keyword])
                })
            }else{
                this.$utils.message.warning(this,'搜索内容不能为空')
            }
            // this.handleClickEvent(e)
        },
        checkPermissionState(){
            this.checkPermission(this.menuname).then(res=>{
                if(res){
                    this.isHide=true
                    this.stateShow=false
                    this.isloading=true
                    //请求数据(缓存了的)
                    if(this.queryData.length){
                     this.successHandle()
                     return true
                    }
                    if(this.$route.query.id){
                     this.getObjectData(this.$route.query.id)
                    }else if(this.$route.query.type){
                     this.getTypeData(this.$route.query.type,this.$route.query.typeid)
                    }else{
                     this.getData(500)
                    }
                    console.log(1);
                }else{
                    console.log(2);
                    this.isloading=false
                    this.isHide=true
                    this.stateShow=true
                    this.stateDescription='暂无权限'
                    this.stateImage=require('@/assets/permission.png')
                }
            });
                
        },
        errorHandle(){
            this.isloading=false
            this.isHide=true
            this.stateShow=true
            this.stateDescription='请求失败'
            this.stateImage=require('@/assets/404.png')
        },
        successHandle(){
            this.isloading=false
            this.stateShow=false
            this.isHide=false
        },
  
      
        
    },
}